package com.camunda.workflow.utils;

import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.Optional;
import java.util.UUID;
import java.util.function.Supplier;

/**
 * @Description TODO
 * @ClassName CommonHelper
 * @Date 2022/3/24 10:01
 * @Author 王东平
 */
@Slf4j
public class CommonHelper {
    private static final String ERROR_STR = "[" + CommonHelper.class.getName() + "]执行方法报错";

    //----------------- try...catch... 处理  start---------------------------------
    public static <T> Optional<T> execute(Supplier<T> tryHandle) {
        return execute(tryHandle, () -> {
        }, () -> {
        });
    }

    public static void execute(Runnable tryHandle) {
        execute(tryHandle, () -> {
        }, () -> {
        });
    }

    public static <T> Optional<T> execute(Supplier<T> tryHandle, Runnable finalHandle) {
        return execute(tryHandle, () -> {
        }, finalHandle);
    }

    public static void execute(Runnable tryHandle, Runnable finalHandle) {
        execute(tryHandle, () -> {
        }, finalHandle);
    }

    public static <T> Optional<T> execute(Supplier<T> tryHandle, Runnable catchHandle, Runnable finalHandle) {
        Optional<T> optional = Optional.empty();
        try {
            optional = Optional.ofNullable(tryHandle.get());
        } catch (Exception e) {
            log.error(ERROR_STR, e);
            catchHandle.run();
            return optional;
        } finally {
            finalHandle.run();
        }
        return optional;
    }

    public static void execute(Runnable tryHandle, Runnable catchHandle, Runnable finalHandle) {
        try {
            tryHandle.run();
        } catch (Exception e) {
            log.error(ERROR_STR, e);
            catchHandle.run();
        } finally {
            finalHandle.run();
        }
    }
    //----------------- try...catch... 处理  end---------------------------------
    //----------------- 驼峰与下划线转换  start---------------------------------

    /**
     * 将驼峰转为下划线
     */
    public static String hump2underline(String str) {
        return StrUtil.toUnderlineCase(str);
//        Pattern compile = Pattern.compile("[A-Z]");
//        Matcher matcher = compile.matcher(str);
//        StringBuffer sb = new StringBuffer();
//        while (matcher.find()) {
//            matcher.appendReplacement(sb, "_" + matcher.group(0).toLowerCase());
//        }
//        matcher.appendTail(sb);
//        return sb.toString();
    }

    /**
     * 将下划线转为驼峰
     */
    public static String underline2hump(String str) {
        return StrUtil.toCamelCase(str);
//        str = str.toLowerCase();
//        Pattern compile = Pattern.compile("_[a-z]");
//        Matcher matcher = compile.matcher(str);
//        StringBuffer sb = new StringBuffer();
//        while (matcher.find()) {
//            matcher.appendReplacement(sb, matcher.group(0).toUpperCase().replace("_", ""));
//        }
//        matcher.appendTail(sb);
//        return sb.toString();
    }
    //----------------- 驼峰与下划线转换  end---------------------------------

    /**
     * 生成随机数
     *
     * @param length 随机数的长度
     */
    public static String randomString(int length) {
        if (length < 4 || length > 20) {
            throw new IllegalArgumentException("非法长度");
        }
        String[] chars = new String[]{"a", "b", "c", "d", "e", "f",
                "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s",
                "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5",
                "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I",
                "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
                "W", "X", "Y", "Z"};
        StringBuilder shortBuffer = new StringBuilder();
        String uuid = UUID.randomUUID().toString().replace("-", "");
        for (int i = 0; i < length; i++) {
            String str = uuid.substring(i + 8, i + 8 + 4);
            int x = Integer.parseInt(str, 16);
            shortBuffer.append(chars[x % 0x3E]);
        }
        return shortBuffer.toString();
    }
}
